package com.bbh.plugin.single.privilege.core.service;

import com.bbh.plugin.single.privilege.core.entity.AccountApp;
import com.bbh.common.domain.Condition;
import com.bbh.plugin.single.privilege.core.entity.Account;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;
import com.bbh.common.base.service.BaseService;
@Service
@Transactional
public class AccountService extends BaseService<Account, String> {
    @Autowired
    private AccountAppService accountAppService;


    public void entryptPassword(Account account) {
        String password = "123456";
        if (StringUtils.isNotBlank(account.getMobile())) {
            if (account.getMobile().length() > 6) {
                password = StringUtils.substring(account.getMobile(), account.getMobile().length() - 6, account.getMobile().length());
            }
        } else if (StringUtils.isBlank(account.getEmail())) {
            password = StringUtils.replace(account.getEmail(), "@", StringUtils.EMPTY);
        }
        password = StringUtils.reverse(password);
        byte[] bytes = password.getBytes();
        String pass = DigestUtils.md5DigestAsHex(bytes);
        account.setPassword(pass);
    }


    public void insertAccountAndAppCode(Account account, String appId) {
        super.insert(account);
        if (StringUtils.isNotEmpty(appId) && appId.length() > 0) {
            String[] appIds = appId.split(",");
            AccountApp accountAppCode;
            for (String id : appIds) {
                accountAppCode = new AccountApp();
                accountAppCode.setAccountId(account.getId());
                accountAppCode.setAppCode(id);
                accountAppCode.setId(UUID.randomUUID().toString().replace("-", ""));
                accountAppCode.setCreateBy(account.getCreateBy());
                accountAppCode.setCreateBy(account.getCreateBy());
                accountAppCode.setCreateTime(account.getCreateTime());
                accountAppCode.setModifyTime(account.getModifyTime());
                accountAppService.insert(accountAppCode);
            }
        }
    }

    public void updateAccountAndAppCode(Account modifyAccount, String appKeys) {
        super.updateByPrimaryKeySelective(modifyAccount);
        if (StringUtils.isNotEmpty(appKeys) && appKeys.length() > 0) {
            String[] appEnames = appKeys.split(",");
            AccountApp accountAppCode;
            Condition condition = new Condition();
            condition.putEquals("accountId", modifyAccount.getId());
            List<AccountApp> accountAppCodeList = accountAppService.selectList(condition, null);
            //先删除
            if (accountAppCodeList.size() > 0) {
                for (AccountApp appKey : accountAppCodeList) {
                    accountAppService.deleteByPrimaryKey(appKey.getId());
                }
            }
            for (String appEname : appEnames) {
                accountAppCode = new AccountApp();
                accountAppCode.setAccountId(modifyAccount.getId());
                accountAppCode.setAppCode(appEname);
                accountAppCode.setId(UUID.randomUUID().toString().replace("-", ""));
                accountAppCode.setCreateTime(modifyAccount.getCreateTime());
                accountAppCode.setModifyTime(modifyAccount.getModifyTime());
                accountAppService.insert(accountAppCode);
            }
        }
    }
}
